home *** CD-ROM | disk | FTP | other *** search
/ CICA 32 1998 April / CICA32 (April 1998) (Disc 4 of 4).iso / utils / unzip531.tgz / unzip531.tar / beos / beos.c next >
C/C++ Source or Header  |  1997-05-31  |  33KB  |  948 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   beos.c
  4.  
  5.   BeOS-specific routines for use with Info-ZIP's UnZip 5.30 and later.
  6.   (based on unix/unix.c)
  7.  
  8.   Contains:  readdir()
  9.              do_wild()           <-- generic enough to put in fileio.c?
  10.              mapattr()
  11.              mapname()
  12.              checkdir()
  13.              mkdir()
  14.              close_outfile()
  15.              version()
  16.              scanBeOSexfield()
  17.              isBeOSexfield()
  18.              set_file_attrs()
  19.              setBeOSexfield()
  20.              printBeOSexfield()
  21.  
  22.   ---------------------------------------------------------------------------*/
  23.  
  24. #define UNZIP_INTERNAL
  25. #include "unzip.h"
  26.  
  27. #include "beos.h"
  28. #include <errno.h>             /* Just make sure we've got a few things... */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32.  
  33. #include <dirent.h>
  34.  
  35. /* For the new post-DR8 file attributes */
  36. #include <fs_attr.h>
  37. int set_file_attrs( const char *, const unsigned char *, const off_t );
  38.  
  39. /* I haven't gotten gcc for DR9 yet... */
  40. #ifdef __GNUC__
  41. #warn GNU C is not supported right now, you have been warned!
  42. #endif
  43.  
  44. static int created_dir;        /* used in mapname(), checkdir() */
  45. static int renamed_fullpath;   /* ditto */
  46.  
  47. #ifndef SFX
  48.  
  49. /**********************/
  50. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  51. /**********************/
  52.  
  53. char *do_wild(__G__ wildspec)
  54.     __GDEF
  55.     char *wildspec;         /* only used first time on a given dir */
  56. {
  57.     static DIR *dir = (DIR *)NULL;
  58.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  59.     static int firstcall=TRUE, have_dirname, dirnamelen;
  60.     struct dirent *file;
  61.  
  62.  
  63.     /* Even when we're just returning wildspec, we *always* do so in
  64.      * matchname[]--calling routine is allowed to append four characters
  65.      * to the returned string, and wildspec may be a pointer to argv[].
  66.      */
  67.     if (firstcall) {        /* first call:  must initialize everything */
  68.         firstcall = FALSE;
  69.  
  70.         /* break the wildspec into a directory part and a wildcard filename */
  71.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL) {
  72.             dirname = ".";
  73.             dirnamelen = 1;
  74.             have_dirname = FALSE;
  75.             wildname = wildspec;
  76.         } else {
  77.             ++wildname;     /* point at character after '/' */
  78.             dirnamelen = wildname - wildspec;
  79.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  80.                 Info(slide, 0x201, ((char *)slide,
  81.                   "warning:  can't allocate wildcard buffers\n"));
  82.                 strcpy(matchname, wildspec);
  83.                 return matchname;   /* but maybe filespec was not a wildcard */
  84.             }
  85.             strncpy(dirname, wildspec, dirnamelen);
  86.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  87.             have_dirname = TRUE;
  88.         }
  89.  
  90.         if ((dir = opendir(dirname)) != (DIR *)NULL) {
  91.             while ((file = readdir(dir)) != (struct dirent *)NULL) {
  92.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  93.                     continue;  /* Unix:  '*' and '?' do not match leading dot */
  94.                 if (match(file->d_name, wildname, 0)) {  /* 0 == case sens. */
  95.                     if (have_dirname) {
  96.                         strcpy(matchname, dirname);
  97.                         strcpy(matchname+dirnamelen, file->d_name);
  98.                     } else
  99.                         strcpy(matchname, file->d_name);
  100.                     return matchname;
  101.                 }
  102.             }
  103.             /* if we get to here directory is exhausted, so close it */
  104.             closedir(dir);
  105.             dir = (DIR *)NULL;
  106.         }
  107.  
  108.         /* return the raw wildspec in case that works (e.g., directory not
  109.          * searchable, but filespec was not wild and file is readable) */
  110.         strcpy(matchname, wildspec);
  111.         return matchname;
  112.     }
  113.  
  114.     /* last time through, might have failed opendir but returned raw wildspec */
  115.     if (dir == (DIR *)NULL) {
  116.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  117.         if (have_dirname)
  118.             free(dirname);
  119.         return (char *)NULL;
  120.     }
  121.  
  122.     /* If we've gotten this far, we've read and matched at least one entry
  123.      * successfully (in a previous call), so dirname has been copied into
  124.      * matchname already.
  125.      */
  126.     while ((file = readdir(dir)) != (struct dirent *)NULL)
  127.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  128.             if (have_dirname) {
  129.                 /* strcpy(matchname, dirname); */
  130.                 strcpy(matchname+dirnamelen, file->d_name);
  131.             } else
  132.                 strcpy(matchname, file->d_name);
  133.             return matchname;
  134.         }
  135.  
  136.     closedir(dir);     /* have read at least one dir entry; nothing left */
  137.     dir = (DIR *)NULL;
  138.     firstcall = TRUE;  /* reset for new wildspec */
  139.     if (have_dirname)
  140.         free(dirname);
  141.     return (char *)NULL;
  142.  
  143. } /* end function do_wild() */
  144.  
  145. #endif /* !SFX */
  146.  
  147.  
  148.  
  149.  
  150.  
  151. /**********************/
  152. /* Function mapattr() */
  153. /**********************/
  154.  
  155. int mapattr(__G)
  156.     __GDEF
  157. {
  158.     ulg tmp = G.crec.external_file_attributes;
  159.  
  160.     switch (G.pInfo->hostnum) {
  161.         case UNIX_:
  162.         case VMS_:
  163.         case BEOS_:
  164.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  165.             return 0;
  166.         case AMIGA_:
  167.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  168.             G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  169.             break;
  170.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  171.         case FS_FAT_:
  172.         case FS_HPFS_:
  173.         case FS_NTFS_:
  174.         case MAC_:
  175.         case ATARI_:             /* (used to set = 0666) */
  176.         case TOPS20_:
  177.         default:
  178.             tmp = !(tmp & 1) << 1;   /* read-only bit --> write perms bits */
  179.             G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  180.             break;
  181.     } /* end switch (host-OS-created-by) */
  182.  
  183.     /* for originating systems with no concept of "group," "other," "system": */
  184.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  185.     G.pInfo->file_attr &= ~tmp;
  186.  
  187.     return 0;
  188.  
  189. } /* end function mapattr() */
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /************************/
  196. /*  Function mapname()  */
  197. /************************/
  198.                              /* return 0 if no error, 1 if caution (filename */
  199. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  200.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  201.     int renamed;             /*  or 10 if out of memory (skip file) */
  202. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  203.     char pathcomp[FILNAMSIZ];    /* path-component buffer */
  204.     char *pp, *cp=(char *)NULL;  /* character pointers */
  205.     char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  206.     int quote = FALSE;           /* flags */
  207.     int error = 0;
  208.     register unsigned workch;    /* hold the character being tested */
  209.  
  210.  
  211. /*---------------------------------------------------------------------------
  212.     Initialize various pointers and counters and stuff.
  213.   ---------------------------------------------------------------------------*/
  214.  
  215.     if (G.pInfo->vollabel)
  216.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  217.  
  218.     /* can create path as long as not just freshening, or if user told us */
  219.     G.create_dirs = (!G.fflag || renamed);
  220.  
  221.     created_dir = FALSE;        /* not yet */
  222.  
  223.     /* user gave full pathname:  don't prepend rootpath */
  224.     renamed_fullpath = (renamed && (*G.filename == '/'));
  225.  
  226.     if (checkdir(__G__ (char *)NULL, INIT) == 10)
  227.         return 10;              /* initialize path buffer, unless no memory */
  228.  
  229.     *pathcomp = '\0';           /* initialize translation buffer */
  230.     pp = pathcomp;              /* point to translation buffer */
  231.     if (G.jflag)                /* junking directories */
  232.         cp = (char *)strrchr(G.filename, '/');
  233.     if (cp == (char *)NULL)     /* no '/' or not junking dirs */
  234.         cp = G.filename;        /* point to internal zipfile-member pathname */
  235.     else
  236.         ++cp;                   /* point to start of last component of path */
  237.  
  238. /*---------------------------------------------------------------------------
  239.     Begin main loop through characters in filename.
  240.   ---------------------------------------------------------------------------*/
  241.  
  242.     while ((workch = (uch)*cp++) != 0) {
  243.  
  244.         if (quote) {                 /* if character quoted, */
  245.             *pp++ = (char)workch;    /*  include it literally */
  246.             quote = FALSE;
  247.         } else
  248.             switch (workch) {
  249.             case '/':             /* can assume -j flag not given */
  250.                 *pp = '\0';
  251.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  252.                     return error;
  253.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  254.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  255.                 break;
  256.  
  257.             case ';':             /* VMS version (or DEC-20 attrib?) */
  258.                 lastsemi = pp;
  259.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  260.                 break;            /*  later, if requested */
  261.  
  262.             case '\026':          /* control-V quote for special chars */
  263.                 quote = TRUE;     /* set flag for next character */
  264.                 break;
  265.  
  266.             default:
  267.                 /* allow European characters in filenames: */
  268.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  269.                     *pp++ = (char)workch;
  270.             } /* end switch */
  271.  
  272.     } /* end while loop */
  273.  
  274.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  275.  
  276.     /* if not saving them, remove VMS version numbers (appended ";###") */
  277.     if (!G.V_flag && lastsemi) {
  278.         pp = lastsemi + 1;
  279.         while (isdigit((uch)(*pp)))
  280.             ++pp;
  281.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  282.             *lastsemi = '\0';
  283.     }
  284.  
  285. /*---------------------------------------------------------------------------
  286.     Report if directory was created (and no file to create:  filename ended
  287.     in '/'), check name to be sure it exists, and combine path and name be-
  288.     fore exiting.
  289.   ---------------------------------------------------------------------------*/
  290.  
  291.     if (G.filename[strlen(G.filename) - 1] == '/') {
  292.         checkdir(__G__ G.filename, GETPATH);
  293.         if (created_dir) {
  294.             if (QCOND2) {
  295.                 Info(slide, 0, ((char *)slide, "   creating: %s\n",
  296.                   G.filename));
  297.             }
  298.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  299.         }
  300.         return 2;   /* dir existed already; don't look for data to extract */
  301.     }
  302.  
  303.     if (*pathcomp == '\0') {
  304.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  305.           G.filename));
  306.         return 3;
  307.     }
  308.  
  309.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  310.     checkdir(__G__ G.filename, GETPATH);
  311.  
  312.     return error;
  313.  
  314. } /* end function mapname() */
  315.  
  316.  
  317.  
  318.  
  319.  
  320. /***********************/
  321. /* Function checkdir() */
  322. /***********************/
  323.  
  324. int checkdir(__G__ pathcomp, flag)
  325.     __GDEF
  326.     char *pathcomp;
  327.     int flag;
  328. /*
  329.  * returns:  1 - (on APPEND_NAME) truncated filename
  330.  *           2 - path doesn't exist, not allowed to create
  331.  *           3 - path doesn't exist, tried to create and failed; or
  332.  *               path exists and is not a directory, but is supposed to be
  333.  *           4 - path is too long
  334.  *          10 - can't allocate memory for filename buffers
  335.  */
  336. {
  337.     static int rootlen = 0;   /* length of rootpath */
  338.     static char *rootpath;    /* user's "extract-to" directory */
  339.     static char *buildpath;   /* full path (so far) to extracted file */
  340.     static char *end;         /* pointer to end of buildpath ('\0') */
  341.  
  342. #   define FN_MASK   7
  343. #   define FUNCTION  (flag & FN_MASK)
  344.  
  345.  
  346.  
  347. /*---------------------------------------------------------------------------
  348.     APPEND_DIR:  append the path component to the path being built and check
  349.     for its existence.  If doesn't exist and we are creating directories, do
  350.     so for this one; else signal success or error as appropriate.
  351.   ---------------------------------------------------------------------------*/
  352.  
  353.     if (FUNCTION == APPEND_DIR) {
  354.         int too_long = FALSE;
  355. #ifdef SHORT_NAMES
  356.         char *old_end = end;
  357. #endif
  358.  
  359.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  360.         while ((*end = *pathcomp++) != '\0')
  361.             ++end;
  362. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  363.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  364.             *(end = old_end + FILENAME_MAX) = '\0';
  365. #endif
  366.  
  367.         /* GRR:  could do better check, see if overrunning buffer as we go:
  368.          * check end-buildpath after each append, set warning variable if
  369.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  370.          * appending.  Clear variable when begin new path. */
  371.  
  372.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  373.             too_long = TRUE;                /* check if extracting directory? */
  374.         if (stat(buildpath, &G.statbuf)) {  /* path doesn't exist */
  375.             if (!G.create_dirs) { /* told not to create (freshening) */
  376.                 free(buildpath);
  377.                 return 2;         /* path doesn't exist:  nothing to do */
  378.             }
  379.             if (too_long) {
  380.                 Info(slide, 1, ((char *)slide,
  381.                   "checkdir error:  path too long: %s\n", buildpath));
  382.                 free(buildpath);
  383.                 return 4;         /* no room for filenames:  fatal */
  384.             }
  385.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  386.                 Info(slide, 1, ((char *)slide,
  387.                   "checkdir error:  can't create %s\n\
  388.                  unable to process %s.\n", buildpath, G.filename));
  389.                 free(buildpath);
  390.                 return 3;      /* path didn't exist, tried to create, failed */
  391.             }
  392.             created_dir = TRUE;
  393.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  394.             Info(slide, 1, ((char *)slide,
  395.               "checkdir error:  %s exists but is not directory\n\
  396.                  unable to process %s.\n", buildpath, G.filename));
  397.             free(buildpath);
  398.             return 3;          /* path existed but wasn't dir */
  399.         }
  400.         if (too_long) {
  401.             Info(slide, 1, ((char *)slide,
  402.               "checkdir error:  path too long: %s\n", buildpath));
  403.             free(buildpath);
  404.             return 4;         /* no room for filenames:  fatal */
  405.         }
  406.         *end++ = '/';
  407.         *end = '\0';
  408.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  409.         return 0;
  410.  
  411.     } /* end if (FUNCTION == APPEND_DIR) */
  412.  
  413. /*---------------------------------------------------------------------------
  414.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  415.     buildpath.
  416.   ---------------------------------------------------------------------------*/
  417.  
  418.     if (FUNCTION == GETPATH) {
  419.         strcpy(pathcomp, buildpath);
  420.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  421.         free(buildpath);
  422.         buildpath = end = (char *)NULL;
  423.         return 0;
  424.     }
  425.  
  426. /*---------------------------------------------------------------------------
  427.     APPEND_NAME:  assume the path component is the filename; append it and
  428.     return without checking for existence.
  429.   ---------------------------------------------------------------------------*/
  430.  
  431.     if (FUNCTION == APPEND_NAME) {
  432. #ifdef SHORT_NAMES
  433.         char *old_end = end;
  434. #endif
  435.  
  436.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  437.         while ((*end = *pathcomp++) != '\0') {
  438.             ++end;
  439. #ifdef SHORT_NAMES  /* truncate name at 14 characters, typically */
  440.             if ((end-old_end) > FILENAME_MAX)      /* GRR:  proper constant? */
  441.                 *(end = old_end + FILENAME_MAX) = '\0';
  442. #endif
  443.             if ((end-buildpath) >= FILNAMSIZ) {
  444.                 *--end = '\0';
  445.                 Info(slide, 0x201, ((char *)slide,
  446.                   "checkdir warning:  path too long; truncating\n\
  447.                    %s\n                -> %s\n", G.filename, buildpath));
  448.                 return 1;   /* filename truncated */
  449.             }
  450.         }
  451.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  452.         return 0;  /* could check for existence here, prompt for new name... */
  453.     }
  454.  
  455. /*---------------------------------------------------------------------------
  456.     INIT:  allocate and initialize buffer space for the file currently being
  457.     extracted.  If file was renamed with an absolute path, don't prepend the
  458.     extract-to path.
  459.   ---------------------------------------------------------------------------*/
  460.  
  461. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  462.  
  463.     if (FUNCTION == INIT) {
  464.         Trace((stderr, "initializing buildpath to "));
  465.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1)) ==
  466.             (char *)NULL)
  467.             return 10;
  468.         if ((rootlen > 0) && !renamed_fullpath) {
  469.             strcpy(buildpath, rootpath);
  470.             end = buildpath + rootlen;
  471.         } else {
  472.             *buildpath = '\0';
  473.             end = buildpath;
  474.         }
  475.         Trace((stderr, "[%s]\n", buildpath));
  476.         return 0;
  477.     }
  478.  
  479. /*---------------------------------------------------------------------------
  480.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  481.     sary; else assume it's a zipfile member and return.  This path segment
  482.     gets used in extracting all members from every zipfile specified on the
  483.     command line.
  484.   ---------------------------------------------------------------------------*/
  485.  
  486. #if (!defined(SFX) || defined(SFX_EXDIR))
  487.     if (FUNCTION == ROOT) {
  488.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  489.         if (pathcomp == (char *)NULL) {
  490.             rootlen = 0;
  491.             return 0;
  492.         }
  493.         if ((rootlen = strlen(pathcomp)) > 0) {
  494.             if (pathcomp[rootlen-1] == '/') {
  495.                 pathcomp[--rootlen] = '\0';
  496.             }
  497.             if (rootlen > 0 && (stat(pathcomp, &G.statbuf) ||
  498.                 !S_ISDIR(G.statbuf.st_mode)))        /* path does not exist */
  499.             {
  500.                 if (!G.create_dirs /* || iswild(pathcomp) */ ) {
  501.                     rootlen = 0;
  502.                     return 2;   /* skip (or treat as stored file) */
  503.                 }
  504.                 /* create the directory (could add loop here to scan pathcomp
  505.                  * and create more than one level, but why really necessary?) */
  506.                 if (mkdir(pathcomp, 0777) == -1) {
  507.                     Info(slide, 1, ((char *)slide,
  508.                       "checkdir:  can't create extraction directory: %s\n",
  509.                       pathcomp));
  510.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  511.                     return 3;  /* failed:  file exists, or 2+ levels required */
  512.                 }
  513.             }
  514.             if ((rootpath = (char *)malloc(rootlen+2)) == (char *)NULL) {
  515.                 rootlen = 0;
  516.                 return 10;
  517.             }
  518.             strcpy(rootpath, pathcomp);
  519.             rootpath[rootlen++] = '/';
  520.             rootpath[rootlen] = '\0';
  521.             Trace((stderr, "rootpath now = [%s]\n", rootpath));
  522.         }
  523.         return 0;
  524.     }
  525. #endif /* !SFX || SFX_EXDIR */
  526.  
  527. /*---------------------------------------------------------------------------
  528.     END:  free rootpath, immediately prior to program exit.
  529.   ---------------------------------------------------------------------------*/
  530.  
  531.     if (FUNCTION == END) {
  532.         Trace((stderr, "freeing rootpath\n"));
  533.         if (rootlen > 0)
  534.             free(rootpath);
  535.         return 0;
  536.     }
  537.  
  538.     return 99;  /* should never reach */
  539.  
  540. } /* end function checkdir() */
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547. /****************************/
  548. /* Function close_outfile() */
  549. /****************************/
  550.  
  551. void close_outfile(__G)    /* GRR: change to return PK-style warning level */
  552.     __GDEF
  553. {
  554.     iztimes zt;
  555.     ush z_uidgid[2];
  556.     unsigned eb_izux_flg;
  557.  
  558. /*---------------------------------------------------------------------------
  559.     If symbolic links are supported, allocate a storage area, put the uncom-
  560.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  561.     link to start with, we shouldn't have to worry about overflowing unsigned
  562.     ints with unsigned longs.
  563.   ---------------------------------------------------------------------------*/
  564.  
  565. #ifdef SYMLINKS
  566.     if (G.symlnk) {
  567.         unsigned ucsize = (unsigned)G.lrec.ucsize;
  568.         char *linktarget = (char *)malloc((unsigned)G.lrec.ucsize+1);
  569.  
  570.         fclose(G.outfile);                      /* close "data" file... */
  571.         G.outfile = fopen(G.filename, FOPR);    /* ...and reopen for reading */
  572.         if (!linktarget || fread(linktarget, 1, ucsize, G.outfile) !=
  573.                            (int)ucsize)
  574.         {
  575.             Info(slide, 0x201, ((char *)slide,
  576.               "warning:  symbolic link (%s) failed\n", G.filename));
  577.             if (linktarget)
  578.                 free(linktarget);
  579.             fclose(G.outfile);
  580.             return;
  581.         }
  582.         fclose(G.outfile);                  /* close "data" file for good... */
  583.         unlink(G.filename);                 /* ...and delete it */
  584.         linktarget[ucsize] = '\0';
  585.         Info(slide, 0, ((char *)slide, "-> %s ", linktarget));
  586.         if (symlink(linktarget, G.filename))  /* create the real link */
  587.             perror("symlink error");
  588.         free(linktarget);
  589.         return;                             /* can't set time on symlinks */
  590.     }
  591. #endif /* SYMLINKS */
  592.  
  593.     fclose(G.outfile);
  594.  
  595. /*---------------------------------------------------------------------------
  596.     Change the file permissions from default ones to those stored in the
  597.     zipfile.
  598.   ---------------------------------------------------------------------------*/
  599.  
  600. #ifndef NO_CHMOD
  601.     if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  602.         perror("chmod (file attributes) error");
  603. #endif
  604.  
  605. /*---------------------------------------------------------------------------
  606.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  607.     time:  adjust base year from 1980 to 1970, do usual conversions from
  608.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  609.     light savings time differences.  If we have a Unix extra field, however,
  610.     we're laughing:  both mtime and atime are ours.  On the other hand, we
  611.     then have to check for restoration of UID/GID.
  612.   ---------------------------------------------------------------------------*/
  613.  
  614.     eb_izux_flg = (G.extra_field ?
  615.                    ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length,
  616.                                     0, &zt, z_uidgid) : 0);
  617.     if (eb_izux_flg & EB_UT_FL_MTIME) {
  618.         TTrace((stderr, "\nclose_outfile:  Unix e.f. modif. time = %ld\n",
  619.           zt.mtime));
  620.     } else {
  621.         zt.mtime = dos_to_unix_time(G.lrec.last_mod_file_date,
  622.                                     G.lrec.last_mod_file_time);
  623.     }
  624.     if (eb_izux_flg & EB_UT_FL_ATIME) {
  625.         TTrace((stderr, "close_outfile:  Unix e.f. access time = %ld\n",
  626.           zt.atime));
  627.     } else {
  628.         zt.atime = zt.mtime;
  629.         TTrace((stderr, "\nclose_outfile:  modification/access times = %ld\n",
  630.           zt.mtime));
  631.     }
  632.  
  633.     /* if -X option was specified and we have UID/GID info, restore it */
  634.     if (G.X_flag && eb_izux_flg & EB_UX2_VALID) {
  635.         TTrace((stderr, "close_outfile:  restoring Unix UID/GID info\n"));
  636.         if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
  637.         {
  638.             Info(slide, 0x201, ((char *)slide,
  639.               "warning:  can't set UID %d and/or GID %d for %s\n",
  640.               z_uidgid[0], z_uidgid[1], G.filename));
  641. /* GRR: change return type to int and set up to return warning after utime() */
  642.         }
  643.     }
  644.  
  645.     /* set the file's access and modification times */
  646.     if (utime(G.filename, (struct utimbuf *)&zt)) {
  647.         Info(slide, 0x201, ((char *)slide,
  648.              "warning:  can't set the time for %s\n", G.filename));
  649.     }
  650.  
  651.     /* handle the BeOS extra field if present */
  652.     {
  653.         void *ptr = scanBeOSexfield( G.extra_field,
  654.                                      G.lrec.extra_field_length );
  655.  
  656.         if( ptr ) {
  657.             setBeOSexfield( G.filename, ptr );
  658.         }
  659.     }
  660.  
  661. } /* end function close_outfile() */
  662.  
  663.  
  664.  
  665. #ifndef SFX
  666.  
  667. /************************/
  668. /*  Function version()  */
  669. /************************/
  670.  
  671. void version(__G)
  672.     __GDEF
  673. {
  674.     sprintf((char *)slide, LoadFarString(CompiledWith),
  675. #ifdef __MWERKS__
  676.       "Metrowerks CodeWarrior", "",
  677. #else
  678. #  ifdef __GNUC__
  679.       "GNU C/C++", "",
  680. #  endif
  681. #endif
  682.       "BeOS ",
  683.  
  684. #ifdef __POWERPC__
  685.       "(PowerPC)",
  686. #else
  687.       /* Some day we may have other architectures... */
  688.       "(unknown)",
  689. #endif
  690.  
  691. #ifdef __DATE__
  692.       " on ", __DATE__
  693. #else
  694.       "", ""
  695. #endif
  696.     );
  697.  
  698.     (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  699.  
  700. } /* end function version() */
  701.  
  702. #endif /* !SFX */
  703.  
  704. /******************************/
  705. /* Extra field functions      */
  706. /******************************/
  707.  
  708. /*
  709. ** Scan the extra fields in extra_field, and look for a BeOS EF; return a
  710. ** pointer to that EF, or NULL if it's not there.
  711. */
  712. uch *scanBeOSexfield( uch *extra_field, unsigned ef_len )
  713. {
  714.     uch *ptr = extra_field;
  715.  
  716.     while( ptr < extra_field + ef_len ) {
  717.         if( isBeOSexfield( ptr ) ) {
  718.             return ptr;
  719.         } else {
  720.             ush  size;
  721.  
  722.             ptr += 2;                   /* skip over the ID         */
  723.             size = makeword( ptr );     /* find the size of this EF */
  724.             ptr += 2;
  725.  
  726.             ptr += size;                /* skip this EF */
  727.         }
  728.     }
  729.  
  730.     return NULL;
  731. }
  732.  
  733. int isBeOSexfield( uch *extra_field )
  734. {
  735.     if( extra_field != NULL ) {
  736.         uch *ptr  = extra_field;
  737.         ush  id   = 0;
  738.         ush  size = 0;
  739.  
  740.         id   = makeword( ptr );
  741.         ptr += 2;
  742.         size = makeword( ptr );
  743.         ptr += 2;
  744.  
  745.         if( id == EF_BE_ID && size >= EF_BE_SIZE ) {
  746.             return TRUE;
  747.         }
  748.     }
  749.  
  750.     return FALSE;
  751. }
  752.  
  753. /* Used by setBeOSexfield():
  754.  
  755. Set a file/directory's attributes to the attributes passed in.
  756.  
  757. If set_file_attrs() fails, an error will be returned:
  758.  
  759.      EOK - no errors occurred
  760.  
  761. (other values will be whatever the failed function returned; no docs
  762. yet, or I'd list a few)
  763. */
  764. int set_file_attrs( const char *name, 
  765.                     const unsigned char *attr_buff, 
  766.                     const off_t attr_size )
  767. {
  768.     int                  retval = EOK;
  769.     unsigned char       *ptr;
  770.     const unsigned char *guard;
  771.     int                  fd;
  772.  
  773.     ptr   = (unsigned char *)attr_buff;
  774.     guard = ptr + attr_size;
  775.  
  776.     fd = open( name, O_RDWR );
  777.     if( fd < 0 ) {
  778.         return errno; /* should it be -fd ? */
  779.     }
  780.  
  781.     while( ptr < guard ) {
  782.         ssize_t              wrote_bytes;
  783.         struct attr_info     fa_info;
  784.         const char          *attr_name;
  785.         const unsigned char *attr_data;
  786.  
  787.         attr_name  = (char *)&(ptr[0]);
  788.         ptr       += strlen( attr_name ) + 1;
  789.  
  790.         memcpy( &fa_info, ptr, sizeof( struct attr_info ) );
  791.         ptr     += sizeof( struct attr_info );
  792.  
  793.         attr_data  = ptr;
  794.         ptr       += fa_info.size;
  795.  
  796.         if( ptr > guard ) {
  797.             /* We've got a truncated attribute. */
  798.             Info(slide, 0x201, ((char *)slide,
  799.                  "warning: truncated attribute\n"));
  800.             break;
  801.         }
  802.  
  803.         wrote_bytes = fs_write_attr( fd, attr_name, fa_info.type, 0, 
  804.                                      attr_data, fa_info.size );
  805.         if( wrote_bytes != fa_info.size ) {
  806.             Info(slide, 0x201, ((char *)slide,
  807.                  "warning: wrote %ld attribute bytes of %ld\n",(unsigned long)wrote_bytes,(unsigned long)fa_info.size));
  808.         }
  809.     }
  810.  
  811.     close( fd );
  812.  
  813.     return retval;
  814. }
  815.  
  816. void setBeOSexfield( char *path, uch *extra_field )
  817. {
  818.     uch *ptr       = extra_field;
  819.     ush  id        = 0;
  820.     ush  size      = 0;
  821.     ulg  full_size = 0;
  822.     uch  flags     = 0;
  823.     uch *attrbuff  = NULL;
  824.     int retval;
  825.  
  826.     if( extra_field == NULL ) {
  827.         return;
  828.     }
  829.  
  830.     /* Collect the data from the extra field buffer. */
  831.     id        = makeword( ptr );    ptr += 2;   /* we don't use this... */
  832.     size      = makeword( ptr );    ptr += 2;
  833.     full_size = makelong( ptr );    ptr += 4;
  834.     flags     = *ptr;               ptr++;
  835.  
  836.     /* Do a little sanity checking. */
  837.     if( flags & EF_BE_FL_BADBITS ) {
  838.         /* corrupted or unsupported */
  839.         Info(slide, 0x201, ((char *)slide,
  840.              "Unsupported flags set for this BeOS extra field, skipping.\n"));
  841.         return;
  842.     }
  843.     if( size <= EF_BE_SIZE ) {
  844.         /* corrupted, unsupported, or truncated */
  845.         Info(slide, 0x201, ((char *)slide,
  846.              "BeOS extra field is %d bytes, should be at least %d.\n",size,EF_BE_SIZE));
  847.         return;
  848.     }
  849.  
  850.     /* Find the BeOS file attribute data. */
  851.     if( flags & EF_BE_FL_NATURAL ) {
  852.         /* Uncompressed data */
  853.         attrbuff = ptr;
  854.     } else {
  855.         /* Compressed data */
  856.         attrbuff = (uch *)malloc( full_size );
  857.         if( attrbuff == NULL ) {
  858.             /* No memory to uncompress attributes */
  859.             Info(slide, 0x201, ((char *)slide,
  860.                  "Can't allocate memory to uncompress file attributes.\n"));
  861.             return;
  862.         }
  863.  
  864.         retval = memextract( __G__ attrbuff, full_size, 
  865.                              ptr, size - EF_BE_SIZE );
  866.         if( retval != PK_OK ) {
  867.             /* error uncompressing attributes */
  868.             Info(slide, 0x201, ((char *)slide,
  869.                  "Error uncompressing file attributes.\n"));
  870.  
  871.             /* Some errors here might not be so bad; we should expect */
  872.             /* some truncated data, for example.  If the data was     */
  873.             /* corrupt, we should _not_ attempt to restore the attrs  */
  874.             /* for this file... there's no way to detect what attrs   */
  875.             /* are good and which are bad.                            */
  876.             free( attrbuff );
  877.             return;
  878.         }
  879.     }
  880.  
  881.     /* Now attempt to set the file attributes on the extracted file. */
  882.     retval = set_file_attrs( path, attrbuff, (off_t)full_size );
  883.     if( retval != EOK ) {
  884.         Info(slide, 0x201, ((char *)slide,
  885.              "Error writing file attributes.\n"));
  886.     }
  887.  
  888.     /* Clean up, if necessary */
  889.     if( attrbuff != ptr ) {
  890.         free( attrbuff );
  891.     }
  892.  
  893.     return;
  894. }
  895.  
  896. void printBeOSexfield( int isdir, uch *extra_field )
  897. {
  898.     uch *ptr       = extra_field;
  899.     ush  id        = 0;
  900.     ush  size      = 0;
  901.     ulg  full_size = 0;
  902.     uch  flags     = 0;
  903.  
  904.     /* Tell picky compilers to be quiet. */
  905.     isdir = isdir;
  906.  
  907.     if( extra_field == NULL ) {
  908.         return;
  909.     }
  910.  
  911.     /* Collect the data from the buffer. */
  912.     id        = makeword( ptr );    ptr += 2;
  913.     size      = makeword( ptr );    ptr += 2;
  914.     full_size = makelong( ptr );    ptr += 4;
  915.     flags     = *ptr;               ptr++;
  916.  
  917.     if( id != EF_BE_ID ) {
  918.         /* not a 'Be' field */
  919.         printf( "\t*** Unknown field type (0x%04x, '%c%c')\n", id,
  920.                 (char)(id >> 8), (char)id );
  921.     }
  922.  
  923.     if( flags & EF_BE_FL_BADBITS ) {
  924.         /* corrupted or unsupported */
  925.         printf( "\t*** Corrupted BeOS extra field:\n" );
  926.         printf( "\t*** unknown bits set in the flags\n" );
  927.         printf( "\t*** (Possibly created by an old version of zip for BeOS.\n" );
  928.     }
  929.  
  930.     if( size <= EF_BE_SIZE ) {
  931.         /* corrupted, unsupported, or truncated */
  932.         printf( "\t*** Corrupted BeOS extra field:\n" );
  933.         printf( "\t*** size is %d, should be larger than %d\n", size, 
  934.                 EF_BE_SIZE );
  935.     }
  936.  
  937.     if( flags & EF_BE_FL_NATURAL ) {
  938.         /* Uncompressed data */
  939.         printf( "\tBeOS extra field data (uncompressed):\n" );
  940.         printf( "\t\t%d data bytes\n", full_size );
  941.     } else {
  942.         /* Compressed data */
  943.         printf( "\tBeOS extra field data (compressed):\n" );
  944.         printf( "\t\t%d compressed bytes\n", size - EF_BE_SIZE );
  945.         printf( "\t\t%d uncompressed bytes\n", full_size );
  946.     }
  947. }
  948.